// pages/detail/index.js
var WxParse = require('../../assets/js/wxParse/wxParse.js')
const request = require('../../assets/js/request.js');
const api = require('../../assets/js/api.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cartList:[],
    id:0,
    goods:{},
    ishidden:true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.id)
    this.setData({
      id: options.id
    })
    this.getLocalData(this.findGood)
  },
  //获取本地存储
  getLocalData: function (callback) {
    wx.getStorage({
      key: 'goodsList',
      success: function (res) {
        console.log(res.data)
        callback(res.data)
       
      },
    })
    
  },
  //找到对应id的商品详情

  findGood: function (data) {
    let that = this
    let myId = that.data.id
    // console.log(that.data.id)
    data.forEach((item) => {
      console.log(item.id)
      if (item.id == myId) {
        console.log(item.name)
        that.setData({
          goods: item
        })
      }
    })
    console.log(that.data.goods.text)
    var article = that.data.goods.text;
    WxParse.wxParse('article', 'html', article, this, 5)
  },
  getColor:function(e){
    console.log(e)
    this.setData({
      colorId:e.currentTarget.dataset.id,
      colorName:e.currentTarget.dataset.name
    })
  },
  inCart:function(){
    let that = this
    //判断colorId存不存在
    if(!that.data.colorId){
      that.setData({
        ishidden: false
      })
      return false
    }
    let cartItem = {
      id:that.data.id,
      colorId: that.data.colorId,
      colorName:that.data.colorName,
    }
    console.log(cartItem)
    var result = that.data.cartList.concat(cartItem)
    that.setData({
      cartList:result,
      ishidden: false
    })
    wx.setStorage({
      key: 'cartList',
      data: that.data.cartList,
    })
    that.success()
  },
  success:function(){
    wx.showToast({
      title: '成功',
      icon: 'success',
      duration: 2000
    })
  },
  hiddenModal:function(){
    let that = this
    that.setData({
      ishidden: true
    })
  },
  modalChoose:function(){
    return false
  },
  choose:function(e){
    this.setData({
      colorId: e.currentTarget.dataset.id,
      colorName: e.currentTarget.dataset.name
    })
  },
  buyNow:function(){
    //判断colorId存不存在
    if (!that.data.colorId) {
      that.setData({
        ishidden: false
      })
      return false
    }
    wx.navigateTo({
      url: '/pages/shop/index',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})